package com.huidu.bitrade.controller.system;

import com.alibaba.fastjson2.JSON;
import com.huidu.bitrade.constant.SystemConfigConstants;
import com.huidu.bitrade.service.SystemConfigService;
import com.huidu.bitrade.vo.ExchangeTradeLimitSetVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("system/config/api")
@RequiredArgsConstructor
@Slf4j
public class SystemConfigApiController {

    private final SystemConfigService systemConfigService;

    @RequestMapping("getConfigValue")
    public String getConfigValue(String configKey){
        log.info("========调用admin服务接口=======");
        return systemConfigService.getConfigValue(configKey);
    }

    /**
     * 获取某个交易对限制配置
     * @return
     */
    @RequestMapping("tradeOrderSet")
    public ExchangeTradeLimitSetVo tradeOrderSet(String symbol){
        log.info("========调用admin服务接口=======");
        String value = systemConfigService.getConfigValue(SystemConfigConstants.EXCHANGE_TRADE_LIMIT);
        if(StringUtils.isEmpty(value)){
            return null;
        }
        List<ExchangeTradeLimitSetVo> exchangeTradeLimitSetVo = JSON.parseArray(value,ExchangeTradeLimitSetVo.class);
        List<String> symbolList = exchangeTradeLimitSetVo.stream().map(ExchangeTradeLimitSetVo::getSymbol).collect(Collectors.toList());
        if(symbolList.contains(symbol)){
            return exchangeTradeLimitSetVo.stream().filter(s->s.getSymbol().equals(symbol)).collect(Collectors.toList()).get(0);
        }
        return null;
    }

    /**
     * 获取赎回率
     * @return
     */
    @RequestMapping("redeemRate")
    public String redeemRate(Long time){
        log.info("========调用admin服务获取赎回率接口=======");
        LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()); // 将Instant对象转换为LocalDateTime对象
        long years = dateTime.until(LocalDateTime.now(), ChronoUnit.YEARS);
        switch ((int) years){
            case 0://不满一年
                return systemConfigService.getConfigValue(SystemConfigConstants.HDZ_INTEREST_UN_YEAR);
            case 1://一年
                return systemConfigService.getConfigValue(SystemConfigConstants.HDZ_INTEREST_ONE_YEAR);
            case 2://两年
                return systemConfigService.getConfigValue(SystemConfigConstants.HDZ_INTEREST_TWO_YEAR);
            case 3://三年
                return systemConfigService.getConfigValue(SystemConfigConstants.HDZ_INTEREST_THREE_YEAR);
            default:
                return "0";
        }
    }

}
